<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
	      v-if="userInfo.role_id != 10"
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>

        <el-row :gutter="15">

      <!--  <el-col :lg="4" :md="12">
          <el-form-item label="姓名学号:">
            <el-input
              clearable
              v-model="where.username"
              placeholder="请输入姓名或学号"/>
          </el-form-item>
        </el-col> -->

       <el-col :lg="4" :md="6">
         <el-form-item label="学年:">
           <el-select
             filterable
             clearable
             v-model="where.annual"
             @change="$forceUpdate()"
             size="small"
             placeholder="-请选择学年-"
             class="ele-block">
             <el-option v-for="item in annualList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
         </el-form-item>
        </el-col>
        <el-col :lg="4" :md="12">
           <el-form-item label="选择年级:">
             <el-select
               filterable
               clearable
               v-model="where.gradeId"
               @change="$forceUpdate(); getClassList()"
               size="small"
               placeholder="-请选择年级-"
               class="ele-block">
               <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
           </el-form-item>
        </el-col>
        <el-col :lg="4" :md="12">
           <el-form-item label="院系:">
             <el-select
               filterable
               clearable
               v-model="where.departmentsId"
               @change="getClassList"
               size="small"
               placeholder="-请选择所属院系-"
               class="ele-block">
               <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
           </el-form-item>
        </el-col>
     <!--   <el-col :lg="4" :md="12">
           <el-form-item label="专业:">
             <el-select
               filterable
               clearable
               v-model="where.professionalId"
               @change="$forceUpdate()"
               size="small"
               placeholder="-请选择专业-"
               class="ele-block">
               <el-option v-for="item in professionalList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
           </el-form-item>
        </el-col> -->

        <el-col :lg="4" :md="12">
           <el-form-item label="班级:">
             <el-select
               filterable
               clearable
               v-model="where.classId"
               @change="$forceUpdate()"
               size="small"
               placeholder="-请选择班级-"
               class="ele-block">
               <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
           </el-form-item>
        </el-col>
        <el-col :lg="3" :md="12">
          <el-form-item label="性别:">
            <el-select
              clearable
              v-model="where.gender"
              placeholder="全部"
              class="ele-fluid">
              <el-option label="男" value="1"/>
              <el-option label="女" value="2"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="4" :md="12">
           <el-form-item label="体测:">
             <el-select
               filterable
               clearable
               v-model="where.taskId"
               @change="$forceUpdate()"
               size="small"
               placeholder="-请选择体测-"
               class="ele-block">
               <el-option v-for="item in matchList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
           </el-form-item>
        </el-col>


      <el-col :lg="4" :md="12">
         <el-form-item label="项目:">
           <el-select
             filterable
             clearable
             v-model="where.projectId"
             @change="$forceUpdate()"
             size="small"
             placeholder="-请选择项目-"
             class="ele-block">
             <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
         </el-form-item>
      </el-col>
      <el-col :lg="6" :md="12">
        <el-form-item label="关键字:">
          <el-input
            clearable
            v-model="where.keywords"
            placeholder="请输入姓名或学籍号"/>
        </el-form-item>
      </el-col>

      <el-col :lg="10" :md="12">
        <el-form-item label="日期:">
          <el-col :lg="8" :md="12">
            <el-date-picker
              type="date"
              class="ele-fluid"
              v-model="where.startTime"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="请选择开始日期"/>
          </el-col>
          <el-col :lg="2" :md="12">
            至
          </el-col>
          <el-col :lg="8" :md="12">
            <el-date-picker
              type="date"
              class="ele-fluid"
              v-model="where.endTime"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="请选择结束日期"/>
          </el-col>
        </el-form-item>
      </el-col>


        </el-row>
        <el-row :gutter="15">


        <el-col :lg="6" :md="12">
          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload">查询
            </el-button>
            <el-button @click="reset">重置</el-button>
          <!--  <el-button
              size="small"
              type="success"
              icon="el-icon-download"
              class="ele-btn-icon"
              @click="exportExcel">导出
            </el-button> -->
          </div>
        </el-col>
      </el-row>


      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">

      <template slot="toolbar">
                <el-button
                v-if="permission.includes('sys:matchyx:scoreimport')"
                  @click="scoreImport(row)"
                  type="success"
                  icon="el-icon-plus"
                  class="ele-btn-icon"
                  size="small">导入成绩
                </el-button>
                  <el-button
                    @click="exportExcel"
                    type="primary"
                    icon="el-icon-download"
                    class="ele-btn-icon"
                    size="small"
                   >导出
                  </el-button>
                </template>

        <!-- 性别列 -->
        <template slot="gender" slot-scope="{row}">
          <el-tag
            :type="['success', 'primary', 'warning'][row.gender-1]"
            size="mini">
            {{ ['男', '女', '保密'][row.gender - 1] }}
          </el-tag>
        </template>


        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="2"/>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
             v-if="row.videoUrlList"
            :underline="false"
            @click="SeeVideo(row)">视频
          </el-link>
          <el-link v-if="row.reportUrl"
             type="primary"
             :underline="false"
             @click="openWeb(row.reportUrl)">体测报告
          </el-link>
          <div class="videoshow" :id="row.videoId" v-show="row.videoshow">
            <span class="videoclose" @click="closevideo(row)">关闭</span>
          <a-modal
             title="视频播放"
             :width="600"
             :visible="viewVideo"
             :footer="null"
             @cancel="handleCancel"
          >
             <video
                 :src="row.videoUrlList"
                  width="500"
                  controls="controls"
             ></video>
          </a-modal>
          </div>
        </template>

      </ele-pro-table>
    </el-card>
    <score-import
    :data="current"
    :visible.sync="showscoreImport"
    @done="reload"/>
  </div>


</template>

<script>
import { mapGetters } from "vuex";
import XLSX from 'xlsx';
import ScoreImport from './score-import';
import {download} from "@/utils/download";
import setting from "@/config/setting";

export default {
  //name: 'Member',
  computed: {
    ...mapGetters(["permission"]),
  },
  components: {ScoreImport},
  data() {
    return {
      // 表格数据接口
      url: '/sport/record?type=4',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'matchName',
          label: '体测',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: 'projectName',
          label: '项目',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
        },

        {
          prop: 'testerName',
          label: '姓名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'testerNumber',
          label: '学籍号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'score',
          label: '运动成绩',
          sortable: 'custom',
          align: 'center',
          width: 150,
          resizable: false,
        },
        {
          prop: 'bmi',
          label: 'BMI',
          sortable: 'custom',
          align: 'center',
          width: 150,
          resizable: false,
        },
        {
          prop: 'gender',
          label: '性别',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
          slot: 'gender'
        },

        {
          prop: 'pingscore',
          label: '评分',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },

        {
          prop: 'grade',
          label: '年级',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'departName',
          label: '院系名称',
          align: 'center',
          width: 80,
          resizable: false,
        },
        {
          prop: 'clazz',
          label: '专业班级',
          align: 'center',
          width: 80,
          resizable: false,
        },
        {
          prop: 'createdDate',
          label: '运动时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 130,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,

      projectList: [],
      gradeList: [],
      departmentsList: [],
      professionalList: [],
      classList: [],
      annualList:[],
      matchList:[],
      viewVideo:false,
      videoUrl:'',
      showscoreImport:false,
      showvid:false,
	  userInfo:{}
    };
  },
  mounted() {
	  this.getUserInfo();
  },
  methods: {
	  /* 获取当前用户信息 */
	  getUserInfo() {
		  if (setting.userUrl) {
			  this.$http.get('index/getUserInfo').then((res) => {
				  const result = setting.parseUser ? setting.parseUser(res.data) : res.data;
				  console.log("===result",result)
				  if (result.code === 0) {
					  // 赋予对象值
					  this.userInfo = result.data
					  if(this.userInfo.role_id != 10){
						  this.getGradeList();// 获取用户信息
					  }
				  } else {
					  this.$message.error(result.msg);
				  }
			  }).catch((e) => {
				  console.error(e);
				  this.$message.error(e.message);
			  });
		  }
	  },
	  openWeb(urls){
		  this.$router.push({path: '/newWebView/newWebView', query: {urls: urls}});
	  },
    scoreImport(row) {
      this.current = row;
      this.showscoreImport = true;
    },
        SeeVideo(row){
          var that = row;
          that.videoshow = true;
          this.$refs.vueRef.play();//播放
        },
        closevideo(row){
          var that = row;
          that.videoshow = false;
        },

        // 取消预览
        handleCancel(e) {
          this.viewVideo = false;
          this.$refs.vueRef.pause();//暂停
        },
      /* 导出数据 */
      exportData() {
        let array = [['ID', '赛事', '项目', '姓名', '学籍号','运动成绩','性别', '评分', '年级', '院系名称', '专业班级', '运动时间']];
        const loading = this.$loading({lock: true});
        this.$http.get('/sport/record?type=4?page=1&limit=100000').then(res => {
          loading.close();
          if (res.data.code === 0) {
            res.data.data.forEach(d => {
              array.push([
                d.id,
                d.matchName,
                d.projectName,
                d.testerName,
                d.testerNumber,
                d.score,
                d.genderStr,
                d.pingscore,
                d.grade,
                d.departName,
                d.clazz,
                d.createdDate,
              ]);
            });
            this.$util.exportSheet(XLSX, array, '赛事记录');
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
    /* 导出数据Excel */
    exportExcel() {
      download('/sport/exportExcel', Object.assign({type: 4},this.where))
      // let info = Object.assign({},this.where);
      // console.log(info)
      // const loading = this.$loading({lock: true});
      // this.$http
      //   .post("/sport/exportExcel?type=4", info)
      //   .then((res) => {
      //     loading.close();
      //     let data = res.data;
      //     if (data.code == 0) {
      //       // 下载文件
      //       window.location.href = data.data;
      //       this.$message({
      //         type: "success",
      //         message: "导出成功",
      //       });
      //     } else {
      //       this.$message.error("导出失败");
      //     }
      //   })
    },

    getGradeList() {
      this.$http.get("/project/getProjectList").then(response => {
        if (response.data.code === 0) {
          this.projectList = response.data.data
        }else {
          this.$message.error(response.data.msg);
        }
      })
      this.$http.get("/match/getMatchList?mtype=2").then(response => {
        if (response.data.code === 0) {
          this.matchList = response.data.data
        }else {
          this.$message.error(response.data.msg);
        }
      })
      this.$http.get("/grade/getGradeList").then(response => {
        if (response.data.code === 0) {
          this.gradeList = response.data.data
        } else {
          this.$message.error(response.data.msg);
        }
      })

      this.$http.get('/professional/getProfessionalsList').then(res => {
        if (res.data.code === 0) {
          this.professionalList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      })

      this.$http.get('/departments/getDepartmentsList').then(res => {
        if (res.data.code === 0) {
          this.departmentsList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      })


      // this.$http.get('/clss/getClassList').then(res => {
      //   if (res.data.code === 0) {
      //     this.classList = res.data.data;
      //   } else {
      //     this.$message.error(res.data.msg);
      //   }
      // })


     this.$http.get('/annual/index?limit=99999').then(res => {
       if (res.data.code === 0) {
         this.annualList = res.data.data;
       } else {
         this.$message.error(res.data.msg);
       }
     })


      this.reload()
    },
    /* 导出数据Excel */


    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    showVideo(url){
      console.log(url);
      // url = row.videoUrlList;
      window.open(url , "_blank");
    },

    /**
     * 获取班级列表
     */
    getClassList() {
      this.classList = []
      this.where.classId = null;
      if(this.where.departmentsId){
        this.$forceUpdate();
      }
      this.$http.post('/clss/getClassList',{gid :this.where.gradeId, yid:this.where.departmentsId}).then(res => {
        if (res.data.code === 0) {
          this.classList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/member/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的会员吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/member/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/member/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>

<style>

  span.el-link--inner {
      background: #A4ADB3;
      color: white;
      font-size: 12px;
      padding: 2px 4px;
      border-radius: 5px;
  }
  .videoshow {
      position: fixed;
      left: 20%;
      top: 20%;
      width: 850px;
      background: #f3f2f2;
  }
  video{
    padding:10px 5px 5px;
    padding-top: 30px;
    width:800px;
  }
  .videoclose{
      position: absolute;
      float: right;
      border-radius: 37px;
      display: block;
      margin-left: 780px;
      margin-top: 3px;
      background: #e6dddd;
      padding: 2px 4px;
  }
  </style>
